import GlobalEvent from "../../../cfw/event/GlobalEvent";
import BaseView from "../../../cfw/mvc/BaseView";
import SDKManager from "../../../sdk/SDKManager";
import { BannerDir } from "../../SDKConfig";


// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class BannerErrorView extends BaseView {

    @property({
        type: cc.Component.EventHandler,
        displayName: "回调函数"
    })
    callback = new cc.Component.EventHandler();

    protected clickState: number = 0;
    start() {
        GlobalEvent.instance().on(GlobalEvent.EVENT_SHOW, this.eventShow, this)
    }


    onDestroy() {
        super.onDestroy()
        SDKManager.getChannel().hideBanner()
        GlobalEvent.instance().off(GlobalEvent.EVENT_SHOW, this.eventShow, this)
    }

    eventShow() {
        SDKManager.getChannel().hideBanner()
        SDKManager.getChannel().loadBanner()
        this.clickState = 2;
    }

    onNextButtonClick() {
        // let matrix: MatrixMgr = SDKManager.getChannel().getMatrix()
        // if (!matrix) {
        //     if (this.callback) {
        //         this.callback.emit([])
        //     }
        //     return;
        // }
        let flag = SDKManager.getChannel().isBannerErrorClick()
        if (flag) {
            switch (this.clickState) {
                case 0:
                    this.clickState = 1;
                    this.setTimeout(() => {

                        console.log(' SDKManager.getChannel().hasBanner() ', SDKManager.getChannel().hasBanner())
                        if (SDKManager.getChannel().hasBanner()) {
                            SDKManager.getChannel().showBanner(0, BannerDir.BOTTOM_MID)
                            // this.isOk = false;
                            let time = SDKManager.getChannel().getBannerDelayTime() * 1000
                            this.setTimeout(() => {
                                // if (cc.isValid(this.node)) {
                                // this.isOk = true;

                                SDKManager.getChannel().hideBanner()
                                this.clickState = 2;
                                // }
                            }, time);
                        } else {
                            this.clickState = 2;
                        }
                    }, 1000);
                    break;
                case 1:

                    break;
                case 2:
                    if (this.callback) {
                        this.callback.emit([])
                    }
                    break;

            }

        } else {
            if (this.callback) {
                this.callback.emit([])
            }
        }


    }

}
